package controller.expertInfo.adminEdit;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.expertInfo.ExpertCompanyServices;
import validator.NotBlankFluentValidator;
import wt.entity.expertInfo.ExpertCompany;

import java.util.Map;

;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller

@RequestMapping("expertInfo/adminEdit/expertCompany")
public class ExpertCompanyController extends BaseController {

    @Autowired
    ExpertCompanyServices expertCompanyServices;

    @RequiresPermissions("expertInfo:adminEdit:show")
    @RequestMapping("index")
    public String index(String expertId, Map map) throws Exception {
        ExpertCompany expertCompany = expertCompanyServices.adminSelectByExpertIdOnly(expertId);
        if (expertCompany == null) {
            expertCompany = new ExpertCompany();
            expertCompany.setExpertId(expertId);
        }
        map.put(StaticConstant.EDIT_DATA, expertCompany);
        return "expertInfo/adminEdit/expertCompany/index";
    }

    @RequiresPermissions("expertInfo:adminEdit:edit")
    @RequestMapping("editDlg")
    public String editDlg(@RequestParam(value = "id", required = false) String id, String expertId, Map map) throws Exception {
        if (StringUtils.isNotEmpty(id)) {
            ExpertCompany expertCompany;

            expertCompany = expertCompanyServices.selectByPrimaryKey(id);

            if (expertCompany == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, expertCompany);
            map.put(StaticConstant.EDIT_URL, "expertInfo/adminEdit/expertCompany/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            ExpertCompany expertCompany = new ExpertCompany();
            expertCompany.setExpertId(expertId);
            map.put(StaticConstant.EDIT_DATA, expertCompany);
            map.put(StaticConstant.EDIT_URL, "expertInfo/adminEdit/expertCompany/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        return "expertInfo/adminEdit/expertCompany/editDlg";
    }

    @RequiresPermissions("expertInfo:adminEdit:edit")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(ExpertCompany expertCompany) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertCompany.getName(), new NotBlankFluentValidator("单位名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());


        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }

        expertCompanyServices.adminInsertSelective(expertCompany);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("expertInfo:adminEdit:edit")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(ExpertCompany expertCompany) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertCompany.getName(), new NotBlankFluentValidator("单位名称"))

                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }

        expertCompanyServices.adminUpdateByPrimaryKeySelective(expertCompany);
        return commonResult.setSuccess();
    }


}
